package com.dayuanit.dy14.atm.demo2.dy14atmweb.controller;

import com.dayuanit.dy14.atm.demo2.dy14atmservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class PageController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/")
    public String toLogin(HttpServletRequest request) {

        session.setAttribute("testxxx", "sxxxx");

        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie :  cookies) {
                if (cookie.getName().equals("username")) {
                    request.setAttribute("username", cookie.getValue());
                    System.out.println(cookie.getValue());
                }

                if (cookie.getName().equals("pwdSign")) {
                    request.setAttribute("pwdSign", cookie.getValue());
                    System.out.println(cookie.getValue());
                }
            }
        }

        System.out.println("pagecontroller...");
        userService.loadUser("tom");
        return "login";
    }

    @RequestMapping(value = "/user/toUserCenter.html")
    public String toUserCenter() {
        return "index";
    }

    @RequestMapping(value = "/card/toOpenaccount.html")
    public String toOpenaccount(HttpSession session) {
        String token = UUID.randomUUID().toString();
        session.setAttribute("token", token);
        return "openaccount";
    }

    @GetMapping(value = "/card/toDeposit.html")
    public String toDeposit() {
        return "deposit";
    }

    @GetMapping(value = "/card/toFlow.html")
    public String toFlow() {
        return "flow";
    }

    @GetMapping(value = "/user/toRegsit.html")
    public String toRegsit() {
        return "sign-up";
    }
}
